package game.core
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import game.core.bullet.Bullet;
	import game.core.objects.Ball;
	import game.core.role.DefaultRole;
	import game.core.util.DisplayUtil;
	import game.core.util.TextFieldUtil;
	import game.event.GameEvent;
	import game.manager.ProfileManager;
	import game.ui.Screen;
	
	import gs.TweenLite;
	import gs.easing.Circ;

	public class GameSystem extends Sprite implements IDispose
	{
		private var _container:Sprite;
		
		public function get container():Sprite {
			return this._container;
		}
		
		public function GameSystem()
		{
			super();
			if(_instance){
				throw new Error("this class instance one time!");
			}
			_instance = this;
			this.initialized();
		}
		
		public function get border():Number {
			return 1;
		}
		
		public function get boundBorder():Number {
			return 40;
		}
		
		public function get contentWidth():Number {
			return this.stage.stageWidth - 2 * boundBorder - border;
		}
		
		public function get conentHeight():Number {
			return this.stage.stageHeight - 2 * boundBorder - border;
		}
		
		private var _screen:Screen;
		public function initialized():void {
			_container = new Sprite();
			_container.name = "container";
			var g:Graphics = _container.graphics;
			g.clear();
			g.lineStyle(border,0x000000);
			g.drawRect(0,0,this.stage.stageWidth - 2 * boundBorder,this.stage.stageHeight - 2 * boundBorder);
			g.endFill();
			_container.x = boundBorder;
			_container.y = boundBorder;
			_screen = new Screen();
			this.addChild(_screen);
			this.addChild(_container);
			this.configEvent();
		}
		
		public function get screen():Screen {
			return this._screen;
		}
		
		/**
		 * 配置一些需要用到的事件
		 */ 
		protected function configEvent():void {
			addEventListener(GameEvent.SCORE_EVENT, scoreHandle);
			addEventListener(GameEvent.TIME_OVER, timeOverHandle);
			this.stage.addEventListener(Event.ENTER_FRAME,run);
		}
		
		private var cid:uint = 0;
		protected function run(event:Event):void {
			if(this._started){
				if(!this.hasGameObjects()){
					trace("pass this game");
				}else {
					
				}
				if(cid == 0){
					cid = setTimeout(this.testinggg,100);
					this._started = false;
				}
			}
		}
		
		private function testinggg():void {
			var ball:Ball = new Ball();
			ball.radius = 45;
			ball.vx = 5;
			ball.vy = -2;
			ball.gravity = 0.4;
			ball.bounce = -1;
			ball.x = this.container.x + (this.container.width - ball.width) / 2;
			ball.y = -(ball.radius);
			ball.dropAction()
			this.addGameObject(ball);
			clearTimeout(cid);
			cid = 0;
		}
		
		private var gameTimer:GameTimer;
		public function addPauseTime(value:Number):void {
			if(!gameTimer){
				gameTimer = new GameTimer(updateStopTimer,stopCallBackFunc);
			}
			gameTimer.addTime(value);
			gameTimer.start();
		}
		
		private function stopCallBackFunc():void {
			screen.updateStopTimer(0);
			this.start();
		}
		
		private function updateStopTimer(value:Number):void {
			screen.updateStopTimer(value);
		}
		
		private var testID:uint;
		private function testing(event:MouseEvent):void {
			var ball:Ball = new Ball();
			ball.radius = 40;
			ball.vx = 5;
			ball.vy = -3;
			ball.gravity = 0.5;
			ball.bounce = -1;
			ball.x = (this.stage.stageWidth - 2 * boundBorder - 40 * 2) / 2;
			ball.y = 0;// 初始化的小球的位置,需要计算正确,应该在整个区域的里面
			this.addGameObject(ball);
			testID = setTimeout(testfunc,1000,ball);
			trace("fun!!!!!!!!");
		}
		
		private function testfunc(value:Ball):void {
			value.pause = false;
			clearTimeout(testID);
		}
		
		public function autoAddBall():void {
			
		}
		
		private static var _instance:GameSystem;
		
		public static function get instance():GameSystem {
			return _instance;
		}
		
		private var _gameObjects:Array = [];// 先留着这个数组,暂时还没想到用的地方
		
		public function addGameObject(value:GameObject):void {
			container.addChild(value);
			_gameObjects.push(value);
			
		}
		
		public function removeGameObject(value:GameObject):void {
			var index:int = _gameObjects.indexOf(value);
			if(index != -1){
				_gameObjects.splice(index,1);
			}
			DisplayUtil.removeChild(container,value);
			value.dispose();
		}
		
		/**
		 * 添加子弹
		 */ 	
		
		private var _bullets:Array = [];
		public function addBullet(value:Bullet):void {
			container.addChildAt(value,0);
			_bullets.push(value);// 子弹有一个消失的时间
		}
		
		/**
		 * 这个方法和上面的方法重复了.
		 */ 
		public function removeBullet(value:Bullet):void {
			var index:int = _bullets.indexOf(value);
			if(index != -1){
				_bullets.splice(index,1);
			}
			DisplayUtil.removeChild(container,value);
			value.dispose();
		}
		
		/**
		 * 暂停
		 * 暂停时有一个时间
		 * 需要考虑到暂停时间的累加
		 * 使用GameTimer去处理这些内容
		 * 可能这里需要传递一些参数,传递的参数主要是说明需要的时间
		 */ 
		private var _isPause:Boolean;
		public function pause():void {
			for each(var gameObject:Object in _gameObjects){
				if(gameObject is IGameObject && (!(gameObject is DefaultRole))){
					IGameObject(gameObject).pause = true;
				}
			}
			_isPause = true;
		}
		
		public function get isPause():Boolean {
			return this._isPause;
		}
		
		/**
		 * 开始
		 */ 
		private var _started:Boolean;
		public function start():void {
			for each(var gameObject:GameObject in _gameObjects){
				gameObject.pause = false;
			}
			this._started = true;
			this._isPause = false;
		}
		
		/**
		 * 检测撞击的物品
		 * 这里的检测还有一些小问题
		 */ 
		public function hitTest(value:Bullet):void {
			for each(var gameObject:GameObject in _gameObjects){
				if(gameObject.collidable && 
					(!(gameObject is IBullet)) && 
					gameObject.hitTestObject(value)){
					if(gameObject is Ball){// 如果是砖等类似的fixed = false的话,那么可能会遇到一种情况
						gameObject.execute();// 分裂
						value.dispose();// 删除
						trace("hit!!!!!!!!!!!!!!!!!!!!!!!");
					}
					break;
				}
			}
		}
		
		/**
		 *分數變化 
		 * @param e
		 * 
		 */		
		protected function scoreHandle(event:GameEvent):void{
			var score:int = int(event.data);
			var textField:TextField = TextFieldUtil.createTextField(score);
			this.container.addChild(textField);
			textField.x = event.target.x;
			textField.y = event.target.y;
			var yy:Number = textField.y - TextFieldUtil.MOVE_PIXELS;
			TweenLite.to(textField,0.5,{y:yy,ease:Circ.easeOut,onComplete:removeTextFieldEffect,onCompleteParams:[textField]});
			ProfileManager.instance.score += score;
		}
		
		private function removeTextFieldEffect(textField:TextField):void {
			DisplayUtil.removeChild(textField.parent,textField);
		
		}
		/**
		 *時間用完 
		 * @param e
		 * 
		 */		
		protected function timeOverHandle(e:GameEvent):void{
			trace("时间到");
			//play end mc
		}
		public function get gameObjects():Array {
			return this._gameObjects;
		}
		
		public function hasGameObjects():Boolean{
			return this._gameObjects.length > 0;
		}
		
		public function get bullets():Array {
			return this._bullets;
		}
		
		public function dispose():void {
			this._gameObjects = [];
			this._bullets = [];
			DisplayUtil.removeAllChildren(_container);
		}
	}
}